package com.hylink.controller;


import com.hylink.base.BaseAction;
import com.hylink.bean.Result;
import com.hylink.bean.ResultCode;
import com.hylink.model.User;
import com.hylink.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/testuser")
public class UserController extends BaseAction {

    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 登录测试 // @ResponseBody 返回数据类型必须得注释 不加此注释跳转页面
     * 使用实体作为参数接收
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public Result login(@RequestBody User user) {
        logger.info("---");

        return new Result(ResultCode.SUCCESS, user);
    }

    /**
     * 返回xml
     * @ResponseBody 注解，它表示此接口响应为纯数据，不带任何界面展示
     * @return
     */
    @RequestMapping(value = "/getUserInfo", produces = { "application/xml;charset=UTF-8" })
    @ResponseBody
    public User getUserInfo() {
        User user = userService.getUserInfo();

        if(user!=null){
            System.out.println("user.getName():"+user.getName());
            logger.info("user.getAge():"+user.getAge());
        }
        return user;
    }

    /**
     * 返回json
     * @return
     */
    @RequestMapping(value = "/getAllUsers", produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public List<User> getAllUsers() {
        List<User> users = userService.getAllUsers();

        return users;
    }



}
